package com.qidian.buy.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qidian.buy.common.pojo.BuyResult;
import com.qidian.buy.pojo.ContentCategory;
import com.qidian.buy.cms.mapper.ContentCategoryMapper;
import com.qidian.buy.cms.service.IContentCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 戴着假发的程序员
 * @since 2024-07-12 10:23:41
 */
@Service
public class ContentCategoryServiceImpl  implements IContentCategoryService {
    @Autowired
    private ContentCategoryMapper categoryMapper;
    @Override
    public BuyResult saveCat(ContentCategory category) {
        // 校验是否重复
        QueryWrapper<ContentCategory> wrapper = new QueryWrapper<>();
        wrapper.eq(category != null && StringUtils.isNoneBlank(category.getCatName()),"cat_name",category.getCatName());
        List<ContentCategory> cats = categoryMapper.selectList(wrapper);
        if(cats != null && cats.size() > 0){
            return BuyResult.duplicate("类型名称重复");
        }
        int result = categoryMapper.insert(category);
        if(result == 1){
            // 判断父类是否需要修改parent属性
            ContentCategory parentCat = categoryMapper.selectById(category.getParentId());
            if(parentCat != null && parentCat.getParent().equals(new Integer(0))){
                parentCat.setParent(1);
                categoryMapper.updateById(parentCat);
            }
        }
        return BuyResult.isSuccess(result);
    }

    @Override
    public BuyResult queryByParentId(Long parentId, int pageNum, int limit) {
//        QueryWrapper<ContentCategory> wrapper = new QueryWrapper<>();
//        wrapper.eq(parentId != null,"parent_id",parentId);
//        IPage<ContentCategory> page = new Page<>(pageNum,limit);
//        return BuyResult.success(categoryMapper.selectPage(page,wrapper));
        return BuyResult.success(categoryMapper.selectPage(new Page<ContentCategory>(pageNum,limit),new QueryWrapper<ContentCategory>().eq(parentId != null,"parent_id",parentId)));
    }

    @Override
    public BuyResult get(Long catId) {
        return BuyResult.success(categoryMapper.selectById(catId));
    }

    @Override
    public BuyResult queryAll() {
        return BuyResult.success(queryByParentId(0L));
    }

    private List<ContentCategory> queryByParentId(Long parentId){
        QueryWrapper<ContentCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        List<ContentCategory> cats = categoryMapper.selectList(wrapper);
        if(cats != null && cats.size() > 0) {
            // 查询子类目
            for (ContentCategory cat : cats) {
                if (cat.getParent().equals(new Integer(1))) {
                    // 有子类目
                    List<ContentCategory> children = queryByParentId(cat.getCatId());
                    cat.setChildren(children);
                }
            }
        }
        return cats;
    }
}
